package com.jiali.backend.controller;

import com.jiali.backend.common.ResponseResult;
import com.jiali.backend.controller.request.AppointmentRequest;
import com.jiali.backend.controller.request.OrderUpdateRequest;
import com.jiali.backend.model.AppointmentOrder;
import com.jiali.backend.service.AppointmentOrderService;
import com.jiali.backend.service.ServiceItemService;
import com.jiali.backend.service.VehicleService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author piko
 * 预约控制层
 */
@RestController
@RequestMapping("/appointment")
public class AppointmentOrderController {
    @Resource
    private AppointmentOrderService appointmentOrderService;
    @Resource
    private VehicleService vehicleService;
    @Resource
    private ServiceItemService serviceItemService;

    @PostMapping("/create")
    public ResponseResult<AppointmentOrder> createAppointment(
            @RequestBody AppointmentRequest request,
            HttpServletRequest httpRequest) {  // 添加HttpServletRequest参数
        return ResponseResult.success(200, "预约成功", appointmentOrderService.createAppointment(request, httpRequest).getData());
    }

    @GetMapping("/list")
    public ResponseResult<List<AppointmentOrder>> getAllAppointments(HttpServletRequest httpRequest) {
        return appointmentOrderService.getAllAppointments(httpRequest);
    }

    @GetMapping("/user/list")
    public ResponseResult<List<AppointmentOrder>> getUserAppointments(
            @RequestParam(required = false) Integer status,
            HttpServletRequest httpRequest) {
        return appointmentOrderService.getUserAppointmentsByStatus(httpRequest, status);
    }

    //订单更新
    @PutMapping("/update")
    public ResponseResult<AppointmentOrder> updateAppointment(@RequestBody OrderUpdateRequest request, HttpServletRequest httpRequest) {
        return appointmentOrderService.updateAppointmentStatus(request, httpRequest);
    }
    @DeleteMapping("/delete")
    public ResponseResult<Boolean> deleteOrder(
            @RequestParam Integer orderId,
            HttpServletRequest request) {
        return appointmentOrderService.deleteOrder(orderId, request);
    }
    @DeleteMapping("/user/cancel")
    public ResponseResult<Boolean> cancelOrderByUser(
            @RequestParam Integer orderId,
            HttpServletRequest request) {
        return appointmentOrderService.cancelOrderByUser(orderId, request);
    }
    @GetMapping("/user/search")
    public ResponseResult<List<AppointmentOrder>> searchOrders(
            @RequestParam(required = false) Integer id,
            @RequestParam(required = false) String model,
            @RequestParam(required = false) String serviceName,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startDate,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endDate,
            @RequestParam(required = false) Integer status,
            HttpServletRequest request) {
        return appointmentOrderService.searchOrders(id, model, serviceName, startDate, endDate, status, request);
    }
}
